package com.example.tsproject.interceptor;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.Teachers;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.Optional;

/**
 * @BelongsProject: springbootdemo
 * @BelongsPackage: com.example.springbootdemo.Interceptor
 * @Author: YoungDra
 * @CreateTime: 2024/08/06  17:34
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override//运行前运行，true放行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
//        response.setHeader("Access-Control-Expose-Headers", "REDIRECT,CONTEXTPATH");
//        //告诉ajax我是重定向
//        response.setHeader("REDIRECT", "REDIRECT");
        String token = request.getHeader("token");
        if (token == null) {
            return true;
        }
        System.out.println(token);
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("YoungDra")).build();
        // 解析指定的token
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        Claim claimsClaim = decodedJWT.getClaim("Claims");
        Map<String,Claim> map=decodedJWT.getClaims();
        System.out.println(map.get("Claims").asMap());
        System.out.println(map.get("Claims").asMap().get("id"));
        System.out.println(map.get("Claims").asMap().get("employee_number"));
        System.out.println(map.get("Claims").asMap().get("student_number"));
        System.out.println(map.get("Claims").asMap().get("name"));
        System.out.println(map.get("Claims").asMap().get("email"));
        int id= (int) map.get("Claims").asMap().get("id");
        String employee_number= (String) map.get("Claims").asMap().get("employee_number");
        String student_number= (String) map.get("Claims").asMap().get("student_number");
        if(employee_number!=null){
            ThreadLocalUtil.set(employee_number);
            String d1=ThreadLocalUtil.get();
            System.out.println("----------------");
            System.out.println(d1);
        }else {
            ThreadLocalUtil.set(student_number);
            String d1=ThreadLocalUtil.get();
            System.out.println("----------------");
            System.out.println(d1);
        }
//        // 获取解析后的token中的payload信息
//        Claim id = decodedJWT.getClaim("id");
//        Claim employeeNumber = decodedJWT.getClaim("employee_number");
//        Claim name = decodedJWT.getClaim("name");
//        Claim email = decodedJWT.getClaim("email");
//
//        System.out.println("ID: " + id.asInt());
//        System.out.println("Employee Number: " + employeeNumber.asString());
//        System.out.println("Name: " + name.asString());
//        System.out.println("Email: " + email.asString());
//        Teachers teachers=new Teachers();
//        teachers.setId(id);
//        teachers.setEmployee_number(employee_number);
//        teachers.setName(name);
//        teachers.setEmail(email);
//        ThreadLocalUtil.set(teachers);


        //告诉ajax我重定向的路径
//        String url = "https://www.baidu.com/";//重定向路径
//        response.setHeader("CONTEXTPATH", url);
//        response.getWriter().write(1);
//        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
//        response.sendRedirect(url);
        return true;
    }

    @Override//运行后运行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    @Override//视图渲染完毕之后，最后运行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
        ThreadLocalUtil.remove();
    }
}
